﻿
using System;
using System.Collections.Generic;
using System.Threading.Tasks;


using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;

namespace CookieApp.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class CookieController : Controller
    {
        [HttpPost]
        [Route("login")]
        public async Task<IActionResult> login([FromForm] string name)
        {
            if (string.IsNullOrEmpty(name)) return Redirect("/login");

            if (name.Equals("admin"))
            {
                var ClaimIdentities = new ClaimsIdentity(new List<Claim>
                {
                    new Claim(ClaimTypes.Name, name)
                }, "auth");

                var claims = new ClaimsPrincipal(ClaimIdentities);

                await HttpContext.SignInAsync(claims);

                return Redirect("/");
            }
            else
            {
                return BadRequest("invalid user");
            }

        }


        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

    }
}
